<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="申请时间" label-width="100">
        <el-date-picker
          v-model="dateRange"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[
            new Date(2000, 1, 1, 0, 0, 0),
            new Date(2000, 1, 1, 23, 59, 59),
          ]"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="销售方" prop="salesName">
        <el-select
            v-model="queryParams.salesName"
            placeholder="销售方"
            @change="handleQuery"
            clearable
          >
            <el-option
              v-for="dict in invoiceSalesList"
              :key="dict.id"
              :label="dict.salesName"
              :value="dict.salesName"
            ></el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="开票状态" prop="invoiceStatus">
        <el-select
            v-model="queryParams.invoiceStatus"
            placeholder="开票状态"
            @change="handleQuery"
            clearable
          >
            <el-option
              v-for="dict in [{name:'未开',vlaue:'未开'},{name:'已开',vlaue:'已开'}]"
              :key="dict.vlaue"
              :label="dict.name"
              :value="dict.vlaue"
            ></el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="开票类型" prop="invoiceType">
        <el-select
            v-model="queryParams.invoiceType"
            placeholder="开票类型"
            @change="handleQuery"
            clearable
          >
            <el-option
              v-for="dict in [{name:'普票',vlaue:'普票'},{name:'专票',vlaue:'专票'}]"
              :key="dict.name"
              :label="dict.name"
              :value="dict.name"
            ></el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input
          v-model="queryParams.email"
          placeholder="请输入邮箱"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        <el-button 
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['business:invoiceApply:export']"
        >导出</el-button>
      </el-form-item>
    </el-form>

<!--    <el-row :gutter="10" class="mb8">-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="Plus"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['business:invoiceApply:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="Edit"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['business:invoiceApply:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="Delete"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['business:invoiceApply:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="Download"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['business:invoiceApply:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
<!--      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>-->
<!--    </el-row>-->

    <el-table v-loading="loading" :data="invoiceApplyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="" align="center" prop="id" v-if="true"/>-->
      <el-table-column label="申请时间" align="center" prop="createTime" />
      <el-table-column label="销售方" align="center" prop="salesName" />
      <el-table-column label="抬头类型" align="center" prop="headerType" />
      <el-table-column label="企业名称/姓名" align="center" prop="companyName" />
      <el-table-column label="税号/身份证号" align="center" prop="taxNumber" />
      <el-table-column label="订单金额" align="center" prop="orderAmount" />
      <el-table-column label="付款截图" align="center" prop="paymentImg" >
        <template #default="scope">
          <div v-for="(url,index) in scope.row.paymentImg ? scope.row.paymentImg.split(',') : 0" :key="url">
            <el-image 
              :preview-teleported="true" 
              :z-index="99999999" 
              :initial-index="index" 
              :preview-src-list="scope.row.paymentImg.split(',')" 
              v-if="url" style="width: 100px; height: 100px" :src="url" fit="contain" />
          </div>
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="邮箱" align="center" prop="email" />
      <el-table-column label="开票备注" align="center" prop="invoiceRemake" />
      <el-table-column label="开票状态" align="center" prop="invoiceStatus" />
      <el-table-column label="开票类型" align="center" prop="invoiceType" />
      <el-table-column label="开票二维码" align="center" prop="invoiceQrCode" >
        <template #default="scope">
          <div v-for="(url,index) in scope.row.invoiceQrCode ? scope.row.invoiceQrCode.split(',') : 0" :key="url">
            <el-image 
              :preview-teleported="true" 
              :z-index="99999999" 
              :initial-index="index" 
              :preview-src-list="scope.row.invoiceQrCode.split(',')" 
              v-if="url" style="width: 100px; height: 100px" :src="url" fit="contain" />
          </div>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remake" />
      <el-table-column label="操作人" align="center" prop="updateBy" />
      <el-table-column label="操作时间" align="center" prop="updateTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['business:invoiceApply:edit']">修改</el-button>
          </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改开票申请记录对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="invoiceApplyRef" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="企业名称/姓名" prop="companyName" >
          <el-input v-model="form.companyName" placeholder="企业名称/姓名" />
        </el-form-item>
        <el-form-item label="税号/身份证号" prop="taxNumber">
          <el-input v-model="form.taxNumber" placeholder="税号/身份证号" />
        </el-form-item>
        <el-form-item label="订单金额" prop="orderAmount">
          <el-input v-model="form.orderAmount" placeholder="订单金额" type="number" />
        </el-form-item>
        <el-form-item label="付款截图" prop="invoiceQrCode">
          <image-uploads v-model="form.paymentImg" limit="9"></image-uploads>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="手机号" type="number" maxlength="11" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="邮箱" type="email" />
        </el-form-item>
        <el-form-item label="开票状态" prop="invoiceQrCode">
          <el-radio-group v-model="form.invoiceStatus">
            <el-radio label="未开">未开</el-radio>
            <el-radio label="已开">已开</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="开票类型" prop="invoiceQrCode">
          <el-radio-group v-model="form.invoiceType">
            <el-radio label="普票">普票</el-radio>
            <el-radio label="专票">专票</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="开票二维码" prop="invoiceQrCode">
          <image-uploads v-model="form.invoiceQrCode" limit="1"></image-uploads>
        </el-form-item>
        <el-form-item label="备注" prop="remake">
          <el-input v-model="form.remake" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="InvoiceApply">
import { listInvoiceApply, getInvoiceApply, delInvoiceApply, addInvoiceApply, updateInvoiceApply } from "@/api/business/invoiceApply";
import ImageUploads from "@/components/ImageUploads/index.vue";
import { listInvoiceSales } from "@/api/business/invoiceSales";
const { proxy } = getCurrentInstance();

const invoiceApplyList = ref([]);
const open = ref(false);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const dateRange = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    salesName: undefined,
    headerType: undefined,
    companyName: undefined,
    taxNumber: undefined,
    orderAmount: undefined,
    paymentImg: undefined,
    phone: undefined,
    email: undefined,
    invoiceRemake: undefined,
    invoiceStatus: undefined,
    invoiceType: undefined,
    invoiceQrCode: undefined,
    remake: undefined,
  },
  rules: {
    id: [
      { required: true, message: "不能为空", trigger: "blur" }
    ],
    salesName: [
      { required: true, message: "销售方不能为空", trigger: "blur" }
    ],
    headerType: [
      { required: true, message: "抬头类型不能为空", trigger: "change" }
    ],
    companyName: [
      { required: true, message: "企业名称/姓名不能为空", trigger: "blur" }
    ],
    taxNumber: [
      { required: true, message: "税号/身份证号不能为空", trigger: "blur" }
    ],
    orderAmount: [
      { required: true, message: "订单金额不能为空", trigger: "blur" }
    ],
    paymentImg: [
      { required: true, message: "付款截图不能为空", trigger: "blur" }
    ],
    phone: [
      { required: true, message: "手机号不能为空", trigger: "blur" }
    ],
    email: [
      { required: true, message: "邮箱不能为空", trigger: "blur" }
    ],
    invoiceRemake: [
      { required: true, message: "开票备注不能为空", trigger: "blur" }
    ],
    invoiceStatus: [
      { required: true, message: "开票状态 0 = 未开，1 = 已开不能为空", trigger: "change" }
    ],
    invoiceType: [
      { required: true, message: "开票类型不能为空", trigger: "change" }
    ],
    invoiceQrCode: [
      { required: true, message: "开票二维码不能为空", trigger: "blur" }
    ],
    // remake: [
    //   { required: true, message: "备注不能为空", trigger: "blur" }
    // ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询开票申请记录列表 */
function getList() {
  loading.value = true;
  listInvoiceApply(proxy.addDateRange(queryParams.value, dateRange.value)).then(response => {
    invoiceApplyList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

const invoiceSalesList = ref([])
function getList2() {
  listInvoiceSales({ pageNum: 1,pageSize: 99999999}).then(response => {
    invoiceSalesList.value = response.rows;
  });
}
// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    headerType: null,
    companyName: null,
    taxNumber: null,
    orderAmount: null,
    paymentImg: null,
    phone: null,
    email: null,
    invoiceRemake: null,
    invoiceStatus: null,
    invoiceType: null,
    invoiceQrCode: null,
    remake: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    delFlag: null,
  };
  proxy.resetForm("invoiceApplyRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  dateRange.value = [];
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加开票申请记录";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  loading.value = true
  reset();
  const _id = row.id || ids.value
  getInvoiceApply(_id).then(response => {
    loading.value = false;
    form.value = response.data;
    open.value = true;
    title.value = "修改开票申请记录";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["invoiceApplyRef"].validate(valid => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id != null) {
        updateInvoiceApply(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        }).finally(() => {
          buttonLoading.value = false;
        });
      } else {
        addInvoiceApply(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        }).finally(() => {
          buttonLoading.value = false;
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除开票申请记录编号为"' + _ids + '"的数据项？').then(function() {
    loading.value = true;
    return delInvoiceApply(_ids);
  }).then(() => {
    loading.value = true;
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  }).finally(() => {
    loading.value = false;
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('business/invoiceApply/export', {
    ...queryParams.value
  }, `invoiceApply_${new Date().getTime()}.xlsx`)
}

getList();
getList2();
</script>
<style scoped>
:deep(.el-upload--picture-card){
  --el-upload-picture-card-size:130px
}
:deep(.el-upload-list--picture-card){
  --el-upload-list-picture-card-size:130px
}

</style>
